package cn.zf233.xcloud.config.common;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

/**
 * Created by zf233 on 2021/5/26
 * RabbitMQ 配置类
 */
@Configuration
@ConditionalOnProperty(prefix = "xcloud", name = {"rabbit", "es"}, havingValue = "true", matchIfMissing = true)
public class RabbitMQConfig implements RabbitListenerConfigurer {

    public static final String XCLOUD_TOPIC_EXCHANGE_OF_ES = "xcloudTopicExchangeOfES";
    public static final String TOPIC_QUEUE_INSERT_OR_UPDATE_OF_ES = "topicQueueInsertOrUpdateOfES";
    public static final String TOPIC_QUEUE_REMOVE_OF_ES = "topicQueueRemoveOfES";
    public static final String ROUTING_KEY_OF_ES_INSERT_OR_UPDATE = "ES.insert.#";
    public static final String ROUTING_KEY_OF_ES_REMOVE = "ES.remove.#";

    @Bean
    public TopicExchange xcloudTopicExchangeOfES() {
        return new TopicExchange(XCLOUD_TOPIC_EXCHANGE_OF_ES);
    }

    @Bean
    public Queue topicQueueInsertOrUpdateOfES() {
        return new Queue(TOPIC_QUEUE_INSERT_OR_UPDATE_OF_ES);
    }

    @Bean
    public Queue topicQueueRemoveOfES() {
        return new Queue(TOPIC_QUEUE_REMOVE_OF_ES);
    }

    @Bean
    public Binding topicBinding01(Queue topicQueueInsertOrUpdateOfES, TopicExchange xcloudTopicExchangeOfES) {
        return BindingBuilder.bind(topicQueueInsertOrUpdateOfES).to(xcloudTopicExchangeOfES).with(ROUTING_KEY_OF_ES_INSERT_OR_UPDATE);
    }

    @Bean
    public Binding topicBinding02(Queue topicQueueRemoveOfES, TopicExchange xcloudTopicExchangeOfES) {
        return BindingBuilder.bind(topicQueueRemoveOfES).to(xcloudTopicExchangeOfES).with(ROUTING_KEY_OF_ES_REMOVE);
    }

    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
        RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
        rabbitMessagingTemplate.setMessageConverter(mappingJackson2MessageConverter());
        rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
        return rabbitMessagingTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
        rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
    }

    @Bean
    MessageHandlerMethodFactory messageHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
        messageHandlerMethodFactory.setMessageConverter(mappingJackson2MessageConverter());
        return messageHandlerMethodFactory;
    }

    @Bean
    public MappingJackson2MessageConverter mappingJackson2MessageConverter() {
        return new MappingJackson2MessageConverter();
    }
}
